package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.OilSampleDetectionBo;
import com.ruoyi.tb.lmt.domain.vo.OilSampleDetectionVo;
import com.ruoyi.tb.lmt.domain.OilSampleDetection;
import com.ruoyi.tb.lmt.mapper.OilSampleDetectionMapper;
import com.ruoyi.tb.lmt.service.IOilSampleDetectionService;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 油压减震器Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-20
 */
@RequiredArgsConstructor
@Service
public class OilSampleDetectionServiceImpl implements IOilSampleDetectionService {

    private final OilSampleDetectionMapper baseMapper;

    @Autowired
    private OilSampleDetectionMapper oilSampleDetectionMapper;

    /**
     * 查询油压减震器
     */
    @Override
    public OilSampleDetectionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public TableDataInfo<OilSampleDetectionVo> queryPageList(OilSampleDetectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OilSampleDetection> lqw = buildQueryWrapper(bo);
        Page<OilSampleDetectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询油压减震器列表
     */
    @Override
    public List<OilSampleDetectionVo> queryList(OilSampleDetectionBo bo) {
        LambdaQueryWrapper<OilSampleDetection> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OilSampleDetection> buildQueryWrapper(OilSampleDetectionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OilSampleDetection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, OilSampleDetection::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), OilSampleDetection::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getOilData()), OilSampleDetection::getOilData, bo.getOilData());
        lqw.eq(StringUtils.isNotBlank(bo.getOilId()), OilSampleDetection::getOilId, bo.getOilId());
        lqw.eq(StringUtils.isNotBlank(bo.getOilType()), OilSampleDetection::getOilType, bo.getOilType());
        lqw.eq(StringUtils.isNotBlank(bo.getBurnMode()), OilSampleDetection::getBurnMode, bo.getBurnMode());
        lqw.eq(StringUtils.isNotBlank(bo.getOilFe()), OilSampleDetection::getOilFe, bo.getOilFe());
        lqw.eq(StringUtils.isNotBlank(bo.getOilCr()), OilSampleDetection::getOilCr, bo.getOilCr());
        lqw.eq(StringUtils.isNotBlank(bo.getOilPb()), OilSampleDetection::getOilPb, bo.getOilPb());
        lqw.eq(StringUtils.isNotBlank(bo.getOilCu()), OilSampleDetection::getOilCu, bo.getOilCu());
        lqw.eq(StringUtils.isNotBlank(bo.getOilSn()), OilSampleDetection::getOilSn, bo.getOilSn());
        lqw.eq(StringUtils.isNotBlank(bo.getOilAl()), OilSampleDetection::getOilAl, bo.getOilAl());
        lqw.eq(StringUtils.isNotBlank(bo.getOilNi()), OilSampleDetection::getOilNi, bo.getOilNi());
        lqw.eq(StringUtils.isNotBlank(bo.getOilAg()), OilSampleDetection::getOilAg, bo.getOilAg());
        lqw.eq(StringUtils.isNotBlank(bo.getOilSi()), OilSampleDetection::getOilSi, bo.getOilSi());
        lqw.eq(StringUtils.isNotBlank(bo.getOilB()), OilSampleDetection::getOilB, bo.getOilB());
        lqw.eq(StringUtils.isNotBlank(bo.getOilNa()), OilSampleDetection::getOilNa, bo.getOilNa());
        lqw.eq(StringUtils.isNotBlank(bo.getOilMg()), OilSampleDetection::getOilMg, bo.getOilMg());
        lqw.eq(StringUtils.isNotBlank(bo.getOilCa()), OilSampleDetection::getOilCa, bo.getOilCa());
        lqw.eq(StringUtils.isNotBlank(bo.getOilBa()), OilSampleDetection::getOilBa, bo.getOilBa());
        lqw.eq(StringUtils.isNotBlank(bo.getOilP()), OilSampleDetection::getOilP, bo.getOilP());
        lqw.eq(StringUtils.isNotBlank(bo.getOilZn()), OilSampleDetection::getOilZn, bo.getOilZn());
        lqw.eq(StringUtils.isNotBlank(bo.getOilMo()), OilSampleDetection::getOilMo, bo.getOilMo());
        lqw.eq(StringUtils.isNotBlank(bo.getOilTi()), OilSampleDetection::getOilTi, bo.getOilTi());
        lqw.eq(StringUtils.isNotBlank(bo.getOilV()), OilSampleDetection::getOilV, bo.getOilV());
        lqw.eq(StringUtils.isNotBlank(bo.getOilMn()), OilSampleDetection::getOilMn, bo.getOilMn());
        lqw.eq(StringUtils.isNotBlank(bo.getOilCd()), OilSampleDetection::getOilCd, bo.getOilCd());
        lqw.eq(StringUtils.isNotBlank(bo.getOilH()), OilSampleDetection::getOilH, bo.getOilH());
        lqw.eq(StringUtils.isNotBlank(bo.getOilC()), OilSampleDetection::getOilC, bo.getOilC());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), OilSampleDetection::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), OilSampleDetection::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, OilSampleDetection::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), OilSampleDetection::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, OilSampleDetection::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增油压减震器
     */
    @Override
    public Boolean insertByBo(OilSampleDetectionBo bo) {
        OilSampleDetection add = BeanUtil.toBean(bo, OilSampleDetection.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改油压减震器
     */
    @Override
    public Boolean updateByBo(OilSampleDetectionBo bo) {
        OilSampleDetection update = BeanUtil.toBean(bo, OilSampleDetection.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OilSampleDetection entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除油压减震器
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean queryExistsByLmtIdAndTestDate(String oilDate, String oilId, String oilType) {
        int count = oilSampleDetectionMapper.queryExistsByLmtIdAndTestDate(oilDate, oilId, oilType);
        return count > 0;
    }


    @Override
    public List<OilSampleDetectionVo> selectOilByLmtNumber(String lmtNumber, Date startDate, Date endDate) {
        return oilSampleDetectionMapper.selectOilByLmtNumber(lmtNumber, startDate, endDate);
    }

    @Override
    public List<OilSampleDetection> selectListByDate(LocalDate date) {
        return oilSampleDetectionMapper.selectListByDate(date);
    }
}
